package com.knightboy.smartshoes.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by 29288 on 2015/9/19.
 */
public class StepDao {
    private SQLiteDatabase db;

    public StepDao(Context context) {
        db = DBHelper.getInstance(context).getWritableDatabase();
    }

    public StepDao(Context context, int version) {
        db = DBHelper.getInstance(context).getWritableDatabase();
    }

    /**
     * 走一步时向数据库中记录
     * @param time
     * @return
     */
    public long insertAStep(String day, String time){
        if(isContent(day,time)){
            return addStep(day,time);
        }
        //否则创建新的一个表项
        ContentValues values = new ContentValues();
        values.put(DBcolumns.STEP_NUM, 1);
        values.put(DBcolumns.STEP_TIME,time);
        long row = db.insert(DBcolumns.TABLE_STEP, null, values);
        return row;
    }

    /**
     * 取出某一天各个时刻的步数
     * @param day 形如
     * @return
     */
    public int[] querySteps(String day){
        int[] steps = getInitArray();
        Cursor cursor = db.query(DBcolumns.TABLE_STEP, new String[] { "*" }, DBcolumns.STEP_DAY + "=?", new String[] { day }, null, null, null);
        if(cursor.getCount() > 0){
            while (cursor.moveToNext()){
                int stepNum = cursor.getInt(cursor.getColumnIndex(DBcolumns.STEP_NUM));
                int index = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DBcolumns.STEP_TIME)));
                steps[index] = stepNum;
            }
            cursor.close();
        }
        return steps;
    }

    /**
     * 查询是否已经存在某个时间的步数，若有，则返回其id
     * @param time
     * @return
     */
    public boolean isContent(String day,String time){
        String queryStr = DBcolumns.STEP_DAY + " = ? and" +  DBcolumns.STEP_TIME + " = ?";
        Cursor cursor = db.query(DBcolumns.TABLE_STEP, new String[] { "*" }, queryStr, new String[] {day,time}, null, null, null);
        boolean flag = false;
        while (cursor.moveToNext()) {
            flag = true;
        }
        cursor.close();
        return flag;
    }

    /**
     * 在已知存在了某个时刻有步数的前提下，给它的步数加1
     * @param time 形如 2015-09-23 12
     */
    public long addStep(String day, String time){
        String[] columns={DBcolumns.STEP_ID, DBcolumns.STEP_NUM};
        String where = DBcolumns.STEP_DAY + " = ? and" +  DBcolumns.STEP_TIME + " = ?";
        String[] parms={day, time};
        Cursor cursor = db.query(DBcolumns.TABLE_STEP, columns, where, parms, null, null, null);
        if (cursor.getCount() != 1){
            return 0;
        }
        cursor.moveToFirst();   //取得第一项
        int id = cursor.getInt(cursor.getColumnIndex(DBcolumns.STEP_ID));
        int stepNum = cursor.getInt(cursor.getColumnIndex(DBcolumns.STEP_NUM));
        cursor.close();
        //取出之后进行修改
        ContentValues values = new ContentValues();
        values.put(DBcolumns.STEP_NUM, stepNum+1);
        long row = db.update(DBcolumns.TABLE_STEP, values, DBcolumns.STEP_ID + " = ?", new String[] {String.valueOf(id)});
        return row;
    }

    /**
     * 获得一个初始化为0的一维数组用来存储一天的步数
     * @return
     */
    private int[] getInitArray(){
        int[] steps = new int[24];
        for(int i = 0; i < 24; i++){
            steps[i] = 0;
        }
        return steps;
    }
}
